package com.test.test.dto.common;


import java.io.Serializable;
import java.util.List;
/**
 * 分页框架统一类
 * 
 * @author liweihu
 * @date 2017年2月10日
 *
 */
@SuppressWarnings("serial")
public class PageResp<T> implements Serializable {

    private int pageNum = 0;
    private int pageSize = 0;
    private int totalcount = 0;
    private List<T> list;

    public PageResp() {
    }

    public PageResp(int pagenum, int totalcount, int pagesize,
                      List<T> list) {
        super();
        this.pageNum = pagenum;
        this.totalcount = totalcount;
        this.pageSize = pagesize;
        this.list = list;
    }

    public int getPageNum() {
        // 获取总页数
        Integer pageCount = getTotalPage();
        // 是否小于1
        pageNum = pageNum < 1 ? 1 : pageNum;
        // 是否大于总页数
        pageNum = pageNum > pageCount ? pageCount : pageNum;
        return pageNum;
    }

    public Integer getPageNo() {
        return getPageNum();
    }

    /**
     * 获取总页数
     *
     * @return
     */
    public Integer getTotalPage() {
        return (int) (getTotalcount() / getPageSize() + (getTotalcount() % getPageSize() == 0 ? 0 : 1));
    }

    public void setPageNum(int pagenum) {
        this.pageNum = pagenum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pagesize) {
        this.pageSize = pagesize;
    }

    public int getTotalcount() {
        return totalcount;
    }

    public void setTotalcount(int totalcount) {
        this.totalcount = totalcount;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    /**
     * 获取当前页记录数
     *
     * @return
     */
    public Integer getCurrentPageSize() {
        return getList().size();
    }

    /**
     * 获取分页开始游标（limit start,pageSize）
     *
     * @return
     */
    public Integer getStart() {
        return (getPageNum() - 1) * getPageSize();
    }

    /**
     * 获取分页结束游标（limit start,pageSize）
     *
     * @return
     */
    public Integer getEnd() {
        return getPageNum() * getPageSize();
    }

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("PageResponse [pagenum=");
		builder.append(pageNum);
		builder.append(", pagesize=");
		builder.append(pageSize);
		builder.append(", totalcount=");
		builder.append(totalcount);
		builder.append(", list=");
		builder.append(list);
		builder.append("]");
		return builder.toString();
	}

}
